package com.hy.appium2;

import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.touch.WaitOptions;
import io.appium.java_client.touch.offset.PointOption;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;
import java.util.concurrent.TimeUnit;

public class AppiumDemo01Swipe {

//    AppiumDriver driver;


    AndroidDriver driver;

    @BeforeMethod
    public void SetUp() throws MalformedURLException {
        //1.需要创建配置对象   desiredcapabilities
        DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
        //将需要启动的软件的一些配置添加进来
        //1.添加需要测试 的主机的路径
        desiredCapabilities.setCapability("deviceName", "127.0.0.1:62001");
        desiredCapabilities.setCapability("platformName", "Android");
        desiredCapabilities.setCapability("appPackage", "com.tencent.qqlite");
        //默认启动不初始化软件
        desiredCapabilities.setCapability("noReset", "true");
        desiredCapabilities.setCapability("appActivity", "com.tencent.mobileqq.activity.SplashActivity");
        //创建驱动
        driver = new AndroidDriver<WebElement>(new URL("http://127.0.0.1:4723/wd/hub"), desiredCapabilities);
//        driver = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), desiredCapabilities);
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }

    @Test
    public void test() throws InterruptedException {

        /**\
         * 210    600
         * 360    600
         * 500     600
         * 360     750
         * 210    890
         * 360    890
         * 500    890
         */

        TouchAction touchAction = new TouchAction(driver);

        PointOption pont1 = PointOption.point(210, 600);
        PointOption pont2 = PointOption.point(360, 600);
        PointOption pont3 = PointOption.point(500, 600);
        PointOption pont4 = PointOption.point(360, 750);
        PointOption pont5 = PointOption.point(210, 890);
        PointOption pont6= PointOption.point(360, 890);
        PointOption pont7 = PointOption.point(500, 890);

        Duration duration = Duration.ofMillis(2000);

        WaitOptions waitOptions = WaitOptions.waitOptions(duration);

        touchAction.press(pont1).waitAction(waitOptions).moveTo(pont2).moveTo(pont3).moveTo(pont4).moveTo(pont5).moveTo(pont6).moveTo(pont7).release().perform();

        Thread.sleep(5000);


    }

    @AfterMethod
    public void TestDown() {
//        driver.quit();
    }
}
